package com.zrrd.gmall.order.receiver;

import com.rabbitmq.client.Channel;
import com.zrrd.gmall.model.enums.ProcessStatus;
import com.zrrd.gmall.model.order.OrderInfo;
import com.zrrd.gmall.order.service.OrderService;
import com.zrrd.gmall.rabbit.constant.MqConstant;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class OrderReceiver {
    @Autowired
    private OrderService orderService;

    //监听
    @RabbitListener(queues = MqConstant.QUEUE_ORDER_CANCEL)
    public  void cancelOrder(Long orderId, Message message, Channel channel) throws IOException {
        System.out.println("收到延迟消息===>"+orderId);
        if (orderId!=null){
            OrderInfo orderInfo =orderService.getById(orderId);
            //查到了而且 订单是未支付
            if (orderInfo!=null && "UNPAID".equals(orderInfo.getOrderStatus())){
                //调用业务层更新订单
                    orderService.updateOrderStatus(orderId, ProcessStatus.CLOSED);
            }
        }
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }
}
